"""
Case Type   : gs_preupgradechk工具
Case Name   : gs_preupgradechk --help对语法参数进行介绍
Create At   : 2025/11/17
Owner       : chen
Description :
    1.gs_preupgradechk --help
Expect      :
    1.工具介绍正常
History     :
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class Tools_Gs_Preupgradechk_TestCase0020(unittest.TestCase):
    def setUp(self):
        LOG.info(f'----{os.path.basename(__file__)} start----')
        self.pri_node = Node('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        LOG.info("---SetUp：检查数据库状态是否正常---")
        status = primary_sh.get_db_cluster_status()
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_gs_preupgradechk_tools(self):
        text = '----step1: gs_preupgradechk --help; ' \
            'expect: 工具介绍正常----'
        LOG.info(text)
        LOG.info(text)
        cmd = f"gs_preupgradechk --help"
        LOG.info(cmd)
        result = self.pri_node.sh(f"source {macro.DB_ENV_PATH};"
                                    f"{cmd}").result()
        LOG.info(result)
        self.assertIn('gs_preupgradechk is a utility to check system resoures' \
            ' before upgrade', result)
        usage_list = ['gs_preupgradechk -? | --help',
            'gs_preupgradechk -V | --version',
            'gs_preupgradechk -i ITEM [-l LOGFILE]']
        for usage in usage_list:
            self.assertIn(usage, result)
        general_list = ['-i', '-l', '-?, --help', '-V --version']
        for general in general_list:
            self.assertIn(general, result)
        item_list = ["'A1':[ cpu idle status ]", 
            "'A2':[ mem used status ]", 
            "'A3':[ disk used status ]", 
            "'A4':[ process status ]", 
            "'A5':[ network status ]", 
            "'A6':[ database status ]", 
            "'A7':[ replaygay status ]"]
        for item in item_list:
            self.assertIn(item, result)

    def tearDown(self):
        LOG.info('--------清理环境--------')
        status = primary_sh.get_db_cluster_status("detail")
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(f"------{os.path.basename(__file__)} end------")
